package net.redxyz.open.api;

import java.util.List;

import org.zhiqim.kernel.util.DateTimes;
import org.zhiqim.kernel.util.Strings;
import org.zhiqim.kernel.util.Validates;
import org.zhiqim.kernel.util.consts.Str;
import org.zhiqim.orm.ORM;
import org.zhiqim.orm.ZTable;
import org.zhiqim.orm.dbo.Selector;
import org.zhiqim.orm.dbo.Updater;

import net.redxyz.open.api.dbo.AppApiForbid;
import net.redxyz.top.dbo.sys.SysApp;

/**
 * 开放API 服务
 * 
 * @version v7.0.1 @author lgz 2021-10-22 新建与整理
 */
public class OpenApiService
{
    
    /**
     * 新增禁用API
     * 
     * @param appKey            应用ID
     * @param method            方法名
     * @param desc              描述
     * @param forbidDesc        禁用描述
     * @throws Exception
     */
    public static void doAddForbidApi(long appKey, String method, String desc, String forbidDesc) throws Exception
    {
        if (appKey <= 0 || Validates.isEmptyBlank(method))
            return;
        
        method = Strings.trim(method);
        AppApiForbid api = ORM.get(ZTable.class).item(AppApiForbid.class, new Selector("appKey", appKey).addMust("apiMethod", method));
        if (api != null)
            return;
        
        api = new AppApiForbid();
        api.setApiDesc(desc);
        api.setApiMethod(method);
        api.setAppKey(appKey);
        api.setApiState(0);
        api.setOpenDesc(null);
        api.setForbidDesc(forbidDesc);
        api.setUpdateTime(DateTimes.getDateTimeString());
        
        ORM.get(ZTable.class).insert(api);
    }
    
    /**
     * 对禁用后的API启用后，再关闭
     * @param appKey            应用ID
     * @param method            方法名
     * @param forbidDesc        禁用
     * @throws Exception
     */
    public static void doForbidApi(long appKey, String method, String forbidDesc) throws Exception
    {
        if (appKey <= 0 || Validates.isEmptyBlank(method))
            return;
     
        Updater upd = new Updater();
        upd.addMust("appKey", appKey);
        upd.addMust("apiMethod", Strings.trim(method));
        
        upd.addField("forbidDesc", forbidDesc);
        upd.addField("apiState", 0);// 0禁用
        upd.addField("updateTime", DateTimes.getDateTimeString());
        
        ORM.get(ZTable.class).update(AppApiForbid.class, upd);
    }
    
    /**
     * 对禁用的API 解除禁用
     * @param appKey            应用ID
     * @param method            方法名
     * @param openDesc          解除禁用描述
     * @throws Exception
     */
    public static void doRelieveForbidApi(long appKey, String method, String openDesc) throws Exception
    {
        if (appKey <= 0 || Validates.isEmptyBlank(method))
            return;
        
        Updater upd = new Updater();
        upd.addMust("appKey", appKey);
        upd.addMust("apiMethod", Strings.trim(method));
        
        upd.addField("openDesc", openDesc);
        upd.addField("apiState", 1);// 1开启
        upd.addField("updateTime", DateTimes.getDateTimeString());
        
        ORM.get(ZTable.class).update(AppApiForbid.class, upd);
    }
    
    /**
     *  初始化 禁用API
     * @param appKey
     */
    public static void doInitForbidApi(long appKey) throws Exception
    {//
        if (appKey <= 0 || ORM.get(ZTable.class).item(SysApp.class, new Selector("appKey", appKey)) == null)
            return;
        
        List<Str> ls = OpenApiConstants.getSysForbidApiList();
        for (Str str : ls)
            doAddForbidApi(appKey, str.value(), str.desc(), "系统高危API,禁止调用。");
    }
}
